package com.neo.service.impl;

import com.neo.bean.*;
import com.neo.dto.AreaDto;
import com.neo.dto.ProvinceDto;
import com.neo.mapper.*;
import com.neo.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaMapper areaMapper;


    @Override
    public AreaDto getAreaById(String parentId) {
        AreaDto areaDto = new AreaDto();
        areaDto.setList(areaMapper.getAreaById(parentId));
        Area area = new Area();
        // 查询省份数量
        area.setParentId(Integer.parseInt(parentId));
        List<Area> areas = areaMapper.select(area);
        areaDto.setProvinceSize(areas.size());
        // 查询市数量
        area.setLevel(1);
        int citySize = areaMapper.selectCount(area);
        // 区县数量
        area.setLevel(2);
        int countrySize = areaMapper.selectCount(area);
        // 乡镇数量
        area.setLevel(3);
        int streetSize = areaMapper.selectCount(area);
        areaDto.setCitySize(citySize);
        areaDto.setCountrySize(countrySize);
        areaDto.setStreetSize(streetSize);
        return areaDto;
    }

    @Override
    public List<ProvinceDto> getAreaByLike(String keyWord) {
        return areaMapper.getAreaByLike(keyWord);
    }

    @Override
    public int updateArea( String name, String code, Integer type, Integer id) {
        Area area;
        if (type == 0) {
            // 用户进行新增操作
            area = new Area();
            area.setName(name);
            area.setCode(code);
            return areaMapper.insertSelective(area);
        }
        // 用户进行修改操作
        area = areaMapper.selectByPrimaryKey(id);
        if (area == null) {
            return -1;
        }
        area.setName(name);
        area.setCode(code);
        return areaMapper.updateByPrimaryKeySelective(area);
    }
}
